<?php
/**
 * @name 系统配置模块
 */
class ConfigAction extends AdminAction{
    public function _initialize() {
        parent::_initialize();  //RBAC 验证接口初始化
    }

	// 基本信息设置
    public function conf(){
		$id           = $this->_get('id','trim','web');
		$config       = require './config.php';	//网站配置
		$config_admin = require APP_PATH.'Conf/Admin/config.php';	//后台分组配置
		$config_home  = require APP_PATH.'Conf/Home/config.php';	//前台分组配置
		$tpl          = './Template/Home/*';		//前台模板
		$list         = glob($tpl);
		foreach($list as $i => $file){
			if (!is_file($file) && $file != "." && $file != ".." )
			$temp[$i]['filename']=basename($file);
		}
		$this->assign('temp',$temp);
		$this->assign('con',$config);
		$this->assign('con_admin',$config_admin);
		$this->assign('con_home',$config_home);
	    $this->display($id);
    }
    
	// 配置信息保存
    private function updateconfig($config){
    	foreach ($config as $k => $c) {
    		$config_old = array();
    		$config_new = array();
    		switch ($k) {
    			case 'con':
					$config_old = require './config.php';
					if(is_array($c)) $config_new = array_merge($config_old,$c);
					arr2file('./config.php',$config_new);
    				break;

    			case 'con_admin':
					$config_old = require APP_PATH.'Conf/Admin/config.php';
					if(is_array($c)) $config_new = array_merge($config_old,$c);
					arr2file(APP_PATH.'Conf/Admin/config.php',$config_new);
    				break;
    			
    			case 'con_home':
					$config_old = require APP_PATH.'Conf/Home/config.php';
					if(is_array($c)) $config_new = array_merge($config_old,$c);
					arr2file(APP_PATH.'Conf/Home/config.php',$config_new);
    				break;
    		}

    	}
    	@unlink('./temp/~app.php');
		$this->success('更新成功！');
	}
	
	//更新web相关配置
    public function updateweb(){
		$con                      = $_POST["con"];
		if(isset($_POST["con_home"]))
		$con_home                 = $_POST["con_home"];
		if(isset($con['web_url']))
		$con['web_url']           = getaddxie($con['web_url']);
		if(isset($con['web_path']))
		$con['web_path']          = getaddxie($con['web_path']);
		if(isset($con['web_adsensepath']))
		$con['web_adsensepath']   = getrexie($con['web_adsensepath']);
		if(isset($con['web_copyright']))
		$con['web_copyright']     = stripslashes($con['web_copyright']);
		if(isset($con['web_tongji']))
		$con['web_tongji']        = stripslashes($con['web_tongji']);
		if(isset($con['web_admin_pagenum']))
		$con['web_admin_pagenum'] = abs(intval($con['web_admin_pagenum']));
		if(isset($con['web_home_pagenum']))
		$con['web_home_pagenum']  = abs(intval($con['web_home_pagenum']));
		if(isset($con['web_adsensepath'])){
			$dir                      = './'.$con['web_adsensepath'];	//广告保存目录
			if(!is_dir($dir)){
				mkdirss($dir);
			}
		}
		if(isset($con_home)){
			$config = array('con'=>$con,'con_home'=>$con_home);
		}else{
			$config = array('con'=>$con);
		}
		$this->updateconfig($config);
	}

	//更新数据库链接配置
    public function updatedb(){
		$con = $_POST["con"];
		$con['db_port'] = abs(intval($con['db_port']));
		$config = array('con'=>$con);
		$this->updateconfig($config);
	}


	/**
	 * 获取微信tpl
	 */
	public function get_weapp_tpl(){
		$id = I('get.id');
		$field = I('get.field');
		import("@.Libary.weapp");
		$option['appid'] = C('weapp.appid');
		$option['appsecret'] = C('weapp.appsecret');
		$option['token'] = C('weapp.token');
		$option['encodingaeskey'] = C('weapp.encodingaeskey');
		$option['debug'] = true;
		$weChat = new Weapp($option);

		$res = $weChat->getTemplate($id);
		$keyword_ids = array();
		$template_titles = array(
			'AT0009' => array('订单编号','商品名称','支付金额','支付方式','订单时间'),
			'AT0007' => array('会员名称','收货地址','订单编号','物流公司','物流单号'),
			'AT0036' => array('订单编号','商品名称','退款金额','退款时间','备注'),//退款通知
			'AT0453' => array('拼团商品','剩余拼团时间','剩余拼团人数','温馨提示'),//参团人数不足提醒
		);

		for( $i =0 ; $i < count($template_titles[$id]); $i++) {
			foreach($res['keyword_list'] as $k => $v) {
				if($template_titles[$id][$i] == $v['name']) {
					$keyword_ids[] = $v['keyword_id'];
					break;
				}
			}
		}

		if(count($keyword_ids) != count($template_titles[$id])) {
			$this->error('模板不匹配,请联系相关技术人员。');
		}

		$ret = $weChat->addTemplateMessage($id,$keyword_ids);
		$config['con']['weapp_tpl'] = C('weapp_tpl');
		if($res) {
			$config['con']['weapp_tpl'][$field] = $ret;
			$this->updateconfig($config);
		}
	}
}